<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2>{{ dialogTitle | translate }}</h2>
  <span class="flex-1"></span>
  <div tb-help="sentNotification"></div>
  <button mat-icon-button
          (click)="cancel()"
          type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
</mat-progress-bar>
<div mat-dialog-content>
  <mat-horizontal-stepper linear #createNotification
                          labelPosition="end"
                          [orientation]="(stepperOrientation | async)"
                          (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="notificationRequestForm">
      <ng-template matStepLabel>{{ 'notification.setup' | translate }}</ng-template>
      <form [formGroup]="notificationRequestForm">
        <div class="flex items-stretch justify-center">
          <tb-toggle-select formControlName="useTemplate" appearance="fill">
            <tb-toggle-option  [value]="false">{{ 'notification.start-from-scratch' | translate }}</tb-toggle-option>
            <tb-toggle-option  [value]="true">{{ 'notification.use-template' | translate }}</tb-toggle-option>
          </tb-toggle-select>
        </div>
        <div *ngIf="notificationRequestForm.get('useTemplate').value; else scratchTemplate">
          <tb-template-autocomplete
            required allowCreate allowEdit
            formControlName="templateId"
            [notificationTypes]="notificationType.GENERAL">
          </tb-template-autocomplete>
          <ng-container *ngTemplateOutlet="recipientsList"></ng-container>
        </div>
        <ng-template #scratchTemplate>
          <ng-container *ngTemplateOutlet="recipientsList"></ng-container>
          <section formGroupName="template">
            <section formGroupName="configuration">
              <div class="delivery-title">
                <div>
                  <label [class.tb-error]="notificationRequestForm.get('template.configuration.deliveryMethodsTemplates').hasError('atLeastOne')"
                         class="tb-title tb-required">{{ "notification.delivery-methods" | translate }}</label>
                  <div class="tb-form-hint" translate>notification.at-least-one-should-be-selected</div>
                </div>
                <button
                  matTooltip="{{ 'notification.refresh-allow-delivery-method' | translate }}"
                  matTooltipPosition="above"
                  (click)="refreshAllowDeliveryMethod()"
                  *ngIf="showRefresh"
                  mat-icon-button>
                  <mat-icon>refresh</mat-icon>
                </button>
              </div>
              <section formGroupName="deliveryMethodsTemplates" class="delivery-methods-container">
                <ng-container *ngFor="let deliveryMethods of notificationDeliveryMethods">
                  <a *ngIf="isInteractDeliveryMethod(deliveryMethods); else deliveryMethod"
                     class="tb-form-panel stroked delivery-method-container tb-pointer interact"
                     [routerLink]="configurationPage(deliveryMethods)"
                     [formGroupName]="deliveryMethods"
                     [matTooltip]="getDeliveryMethodsTooltip(deliveryMethods)"
                     matTooltipPosition="above">
                    <mat-slide-toggle class="delivery-method" formControlName="enabled">
                      {{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}
                    </mat-slide-toggle>
                    <mat-icon *ngIf="isInteractDeliveryMethod(deliveryMethods)">
                      chevron_right
                    </mat-icon>
                  </a>
                  <ng-template #deliveryMethod>
                    <section class="delivery-method-container tb-form-panel stroked"
                             [formGroupName]="deliveryMethods"
                             [matTooltip]="getDeliveryMethodsTooltip(deliveryMethods)"
                             [matTooltipDisabled]="getDeliveryMethodsTemplatesControl(deliveryMethods).enabled"
                             matTooltipPosition="above">
                      <mat-slide-toggle class="delivery-method" formControlName="enabled">
                        {{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}
                      </mat-slide-toggle>
                    </section>
                  </ng-template>
                </ng-container>
              </section>
            </section>
          </section>
        </ng-template>
        <ng-template #recipientsList>
          <tb-entity-list
            syncIdsWithDB
            required
            formControlName="targets"
            labelText="{{ 'notification.recipients' | translate }}"
            placeholderText="{{ 'notification.recipient' | translate }}"
            requiredText="{{ 'notification.recipients-required' | translate }}"
            entityType="{{ entityType.NOTIFICATION_TARGET }}"
            subType="{{ notificationType.GENERAL }}">
            <button #createTargetButton
                    mat-button color="primary" matSuffix
                    (click)="createTarget($event, createTargetButton)">
              <span style="white-space: nowrap">{{ 'notification.create-new' | translate }}</span>
            </button>
          </tb-entity-list>
        </ng-template>
        <section formGroupName="additionalConfig" class="tb-form-panel stroked no-padding-bottom no-gap">
          <mat-slide-toggle formControlName="enabled" class="toggle">
            {{ 'notification.scheduler-later' | translate }}
          </mat-slide-toggle>
          <div *ngIf="notificationRequestForm.get('additionalConfig.enabled').value" class="flex flex-col">
            <tb-timezone-select userTimezoneByDefault
                                required
                                formControlName="timezone">
            </tb-timezone-select>
            <mat-form-field>
              <mat-label translate>notification.time</mat-label>
              <mat-datetimepicker-toggle [for]="startTimePicker" matPrefix></mat-datetimepicker-toggle>
              <mat-datetimepicker #startTimePicker type="datetime" openOnFocus="true"></mat-datetimepicker>
              <input required matInput class="flex-1" formControlName="time" [min]="minDate()" [max]="maxDate()" [matDatetimepicker]="startTimePicker">
            </mat-form-field>
          </div>
        </section>
      </form>
    </mat-step>
    <mat-step *ngIf="!notificationRequestForm.get('useTemplate').value"
              [stepControl]="notificationTemplateConfigurationForm" #composeStep=matStep>
      <ng-template matStepLabel>{{ 'notification.compose' | translate }}</ng-template>
      <ng-template matStepContent>
        <form [formGroup]="notificationTemplateConfigurationForm">
          <tb-template-configuration
            notificationType="{{ notificationType.GENERAL }}"
            [predefinedDeliveryMethodsTemplate]="deliveryConfiguration"
            [interacted]="composeStep.interacted"
            formControlName="deliveryMethodsTemplates">
          </tb-template-configuration>
        </form>
      </ng-template>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>{{ 'notification.review' | translate }}</ng-template>
      <div class="tb-loader" *ngIf="(isLoading$ | async) && !preview">
        <mat-progress-spinner color="warn" mode="indeterminate"
                              strokeWidth="5">
        </mat-progress-spinner>
      </div>
      <div *ngIf="preview" class="tb-form-panel no-padding no-border">
        <section class="preview-group tb-form-panel stroked no-gap">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">supervisor_account</tb-icon>
            <div class="tb-form-panel-title">{{ 'notification.recipients-count' | translate : {count: preview.totalRecipientsCount} }}</div>
          </div>
          <div class="details-recipients" *ngIf="notificationRequestForm.get('targets').value?.length > 1">
            <div *ngFor="let detail of preview.recipientsCountByTarget | keyvalue" class="details-recipient">
              <span class="number">{{ detail.value }}</span>{{ detail.key }}
            </div>
          </div>
          <mat-divider class="divider"></mat-divider>
          <mat-chip-listbox>
            <mat-chip *ngFor="let recipientTitle of preview.recipientsPreview">
              <span>{{ recipientTitle }}</span>
            </mat-chip>
          </mat-chip-listbox>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.WEB?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:bell-badge</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.web-preview</div>
          </div>
          <div class="web-preview">
            <tb-notification preview [notification]="preview.processedTemplates.WEB"></tb-notification>
          </div>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.MOBILE_APP?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:cellphone-text</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.mobile-app-preview</div>
          </div>
          <div class="notification-content">
            <div class="subject">{{ preview.processedTemplates.MOBILE_APP.subject }}</div>
            <div>{{ preview.processedTemplates.MOBILE_APP.body }}</div>
          </div>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.SMS?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:message-processing</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.sms-preview</div>
          </div>
          <div class="notification-content">
            {{ preview.processedTemplates.SMS.body }}
          </div>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.EMAIL?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:email</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.email-preview</div>
          </div>
          <div class="notification-content">
            <div class="subject">{{ preview.processedTemplates.EMAIL.subject }}</div>
            <mat-divider></mat-divider>
            <editor [init]="tinyMceOptions" [initialValue]="preview.processedTemplates.EMAIL.body"></editor>
          </div>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.SLACK?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:slack</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.slack-preview</div>
          </div>
          <div class="notification-content">
            {{ preview.processedTemplates.SLACK.body }}
          </div>
        </section>
        <section class="preview-group notification tb-form-panel stroked no-gap" *ngIf="preview.processedTemplates.MICROSOFT_TEAMS?.enabled">
          <div class="flex flex-row items-center justify-start gap-2">
            <tb-icon class="tb-mat-18">mdi:microsoft-teams</tb-icon>
            <div class="tb-form-panel-title" translate>notification.delivery-method.microsoft-teams-preview</div>
          </div>
          <div class="notification-content mini" [style.background-color]="preview.processedTemplates.MICROSOFT_TEAMS?.themeColor">
            <div class="subject">{{ preview.processedTemplates.MICROSOFT_TEAMS.subject }}</div>
            {{ preview.processedTemplates.MICROSOFT_TEAMS.body }}
            <button mat-raised-button>{{ preview.processedTemplates.MICROSOFT_TEAMS.button.text }}</button>
          </div>
        </section>
      </div>
    </mat-step>
  </mat-horizontal-stepper>
</div>
<mat-divider></mat-divider>
<div mat-dialog-actions class="tb-dialog-actions">
  <button mat-stroked-button *ngIf="selectedIndex > 0"
          (click)="backStep()">{{ 'action.back' | translate }}</button>
  <span class="flex-1"></span>
  <button mat-raised-button
          color="primary"
          (click)="nextStep()">{{ nextStepLabel() | translate }}</button>
</div>
